package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.demo.dto.base.BaseIdReq;
import com.example.demo.dto.req.UserInsReq;
import com.example.demo.dto.req.UserSearchReq;
import com.example.demo.dto.req.UserUpdReq;
import com.example.demo.mapper.UserMapper;
import com.example.demo.model.User;
import com.example.demo.service.IUserService;
import com.example.demo.util.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * (User)表服务实现类
 *
 * @author wen
 * @since 2022-06-10 09:56:46
 */
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public ApiResult<List<User>> getUserList(UserSearchReq req) {
        LambdaQueryWrapper<User> query = new LambdaQueryWrapper<>();
        if (null != req.getName() && !"".equals(req.getName())) {
            query.like(User::getName, req.getName());
        }
        List<User> users = userMapper.selectList(query);
        return ApiResult.succeed(users);
    }

    @Override
    public ApiResult<Void> insertUser(UserInsReq req) {
        if (null != req) {
            User user = new User();
            user.setName(req.getName());
            user.setPassword(req.getPassword());
            userMapper.insert(user);
            return ApiResult.succeed();
        }
        return ApiResult.failed();
    }

    @Override
    public ApiResult<Void> updateUser(UserUpdReq req) {
        if (null != req) {
            User user = new User();
            user.setId(req.getId());
            user.setName(req.getName());
            user.setPassword(req.getPassword());
            userMapper.updateById(user);
            return ApiResult.succeed();
        }
        return ApiResult.failed();
    }

    @Override
    public ApiResult<Void> delUser(BaseIdReq req) {
        userMapper.deleteById(req.getId());
        return ApiResult.succeed();
    }
}
